import db from '~/DB';
import { getTokenInfo } from './token';

import { defineResponseResolver } from '~~/mocks/define';
import { RestRequest } from 'msw';
import { User } from '~~/DB/types/user.d';
// type T =

interface T extends RestRequest {
  user?: User;
}

export async function getUser(req: T) {
  if (!req.user) {
    const tokenInfo = await getTokenInfo(req);
    if (tokenInfo) {
      const user = await db.users.get({
        uid: tokenInfo.uid,
      });
      req.user = user;
    }
  }
  return req.user;
}
